//
//  NSURL+Utils.m
//  KKMY2_U
//
//  Created by 黄磊 on 14-6-20.
//  Copyright (c) 2014年 jiulong.zhou. All rights reserved.
//

#import "NSURL+Utils.h"

@implementation NSURL (Utils)

- (NSString *)getUrlParameter:(NSString *)param
{
    
    NSError *error;
    NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)",param];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                                                                 error:&error];

     // 执行匹配的过程
     NSArray *matches = [regex matchesInString:self.absoluteString
                                       options:0
                                         range:NSMakeRange(0, [self.absoluteString length])];
     for (NSTextCheckingResult *match in matches) {
             //NSRange matchRange = [match range];
             //NSString *tagString = [webaddress substringWithRange:matchRange];  // 整个匹配串
     //        NSRange r1 = [match rangeAtIndex:1];
     //        if (!NSEqualRanges(r1, NSMakeRange(NSNotFound, 0))) {    // 由时分组1可能没有找到相应的匹配，用这种办法来判断
     //            //NSString *tagName = [webaddress substringWithRange:r1];  // 分组1所对应的串
     //            return @"";
     //        }
    
             NSString *tagValue = [self.absoluteString substringWithRange:[match rangeAtIndex:2]];  // 分组2所对应的串
             //    NSLog(@"分组2所对应的串:%@\n",tagValue);
             return tagValue;
         }
     return @"";
}

@end
